﻿print "Welcome to ArenaRaidIcons Version 12.29.2013"

local inbgtimer = 0
local total = 0
local alreadywhispered = false
local leaderoutside = false

friends = {}

-- 1 = Yellow 4-point Star
friends["Katsamoto"] = 1
friends["Protectorqt"] = 1

-- 2 = Orange Circle
friends["Bannen"] = 2
friends["Cgy"] = 2
friends["Swapdot"] = 2

-- 3 = Purple Diamond
friends["Cardasnipes"] = 3
friends["Arysei"] = 3

-- 4 = Green Triangle
friends["Dezardria"] = 4
friends["Líkeaßoss"] = 4
friends["Envywar"] = 4

-- 5 = White Crescent Moon
friends["Lingø"] = 5
friends["Lucan Malory"] = 5
friends["Wargumby"] = 5
friends["Wrekkin"] = 5

-- 6 = Blue Square
friends["Entyune"] = 6
friends["Shiftctrlesc"] = 6
friends["Shotsshots"] = 6
friends["Slamchop"] = 6
friends["Noyuu"] = 6
friends["Enty"] = 6
friends["Bamboototem"] = 6
friends["Sphereward"] = 6
friends["Cloakvanish"] = 6

-- 7 = Red "X" Cross
friends["Xõrcist"] = 7
friends["Xõrcíst"] = 7
friends["Xórcist"] = 7
friends["Xórcíst"] = 7
friends["Xórcýst"] = 7
friends["Põqweíst"] = 7
friends["Xorcìst"] = 7

-- 8 = White Skull
friends["Cravenfear"] = 8
friends["Chemicáls"] = 8
friends["Chemshot"] = 8

function enteredbghandler()
	print "Entered BG"
	inbgtimer = 0
	alreadywhispered = false
end

-- function groupjoinedhandler()
	-- if ( UnitIsGroupLeader("player") ) then leaderoutside = true end
	-- print "Joined group"
	-- print (leaderoutside)
-- end

local function settargets()
	for name,mark in pairs(friends) do 
		if ( GetRaidTargetIndex(name) ~= mark ) then
			--print (name, mark)
			SetRaidTarget(name,mark)
		end
	end
end

local function givexlead()
	if ( not UnitIsRaidOfficer("Xõrcist") and UnitIsGroupLeader("player") ) then  PromoteToAssistant("Xõrcist") end
	if ( not UnitIsRaidOfficer("Xõrcíst") and UnitIsGroupLeader("player") ) then  PromoteToAssistant("Xõrcíst") end
	if ( not UnitIsRaidOfficer("Xórcist") and UnitIsGroupLeader("player") ) then  PromoteToAssistant("Xórcist") end
	if ( not UnitIsRaidOfficer("Xórcíst") and UnitIsGroupLeader("player") ) then  PromoteToAssistant("Xórcíst") end
	if ( not UnitIsRaidOfficer("Xórcýst") and UnitIsGroupLeader("player") ) then  PromoteToAssistant("Xórcýst") end
end

--Make sure we are leader
if ( UnitIsGroupLeader("player") ) then leaderoutside = true end

--Register the BG handler
local bgframe = CreateFrame("Frame")
bgframe:RegisterEvent("PLAYER_ENTERING_BATTLEGROUND")
bgframe:SetScript("OnEvent", enteredbghandler)

--Register the joined group handler
-- local leaderframe = CreateFrame("Frame")
-- leaderframe:RegisterEvent("GROUP_JOINED")
-- leaderframe:SetScript("OnEvent", groupjoinedhandler)

local function onUpdate(self,elapsed)
    total = total + elapsed
    if total >= 2 then 																					--every two seconds, do stuff
		if ( UnitIsGroupLeader("player") or UnitIsRaidOfficer("player") ) then							--if we are leader or assistant, then promote xorcist and set target
			settargets()
			givexlead()
		else																							--whisper only once after we're in a battleground for 20 seconds to get lead
			if ( ( select(2,IsInInstance()) == 'pvp' and not IsRatedBattleground() ) and ( inbgtimer >= 20 and alreadywhispered == false ) ) then
				print "Tried to whisper"
				print("inbgtimer", inbgtimer, "alreadywhisp",alreadywhispered, "Leader?", UnitIsGroupLeader("player"), "leaderoutside == ", leaderoutside)
				SendChatMessage("can you give me leader or assist so I can set raid targets on my group?" ,"WHISPER" ,"Common" , GetUnitName("raid1",true))
				alreadywhispered = true
			end
		end
		inbgtimer = inbgtimer + 2
		--print("inbgtimer", inbgtimer, "alreadywhisp",alreadywhispered, "Leader?", UnitIsGroupLeader("player"), "leaderoutside == ", leaderoutside)
		total = 0
	end
end

local updaterframe = CreateFrame("frame")
updaterframe:SetScript("OnUpdate", onUpdate)